package com.fr.report.core;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.report.cellElement.Formula;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.core.parser.UtilEvalError;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract interface SegmentHandler
{
  public abstract void stringSegment(String paramString);

  public abstract void parameterSegment(String paramString1, String paramString2);

  public static class QueryCreator
    implements SegmentHandler
  {
    private Calculator c;
    private StringBuffer sb = new StringBuffer();

    public QueryCreator(Calculator paramCalculator)
    {
      if (paramCalculator == null)
        paramCalculator = Calculator.createCalculator();
      this.c = paramCalculator;
    }

    public String getNewQuery()
    {
      return this.sb.toString();
    }

    public void parameterSegment(String paramString1, String paramString2)
    {
      Object localObject = this.c.resolveVariable(paramString1);
      if (localObject == null)
        localObject = paramString2;
      if ((localObject instanceof String) && (((String)localObject).startsWith("=")))
        localObject = new Formula((String)localObject);
      if (localObject instanceof Formula)
        try
        {
          localObject = this.c.eval(((Formula)localObject).getContent());
        }
        catch (UtilEvalError localUtilEvalError)
        {
          FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
        }
      if (localObject instanceof Date)
      {
        str = null;
        if (StringUtils.isBlank(str))
          str = "yyyy-MM-dd";
        localObject = new SimpleDateFormat(str).format((Date)localObject);
      }
      String str = null;
      if (localObject instanceof String)
        str = (String)localObject;
      else if (localObject != null)
        str = localObject.toString();
      if (StringUtils.isNotEmpty(str))
        this.sb.append(str);
    }

    public void stringSegment(String paramString)
    {
      this.sb.append(paramString);
    }
  }

  public static final class ParameterCollector
    implements SegmentHandler
  {
    List parameterList = new ArrayList();

    public Parameter[] getParameters()
    {
      return ((Parameter[])this.parameterList.toArray(new Parameter[this.parameterList.size()]));
    }

    public void parameterSegment(String paramString1, String paramString2)
    {
      this.parameterList.add(new Parameter(paramString1, paramString2));
    }

    public void stringSegment(String paramString)
    {
    }
  }
}